package cn.itcast.web.controller.dept;

import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;
    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1")
         Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize, ModelMap modelMap){
      String companyId=getLoginCompanyId();
        PageInfo<Dept> pageInfo = deptService.findByPage(companyId,pageNum,pageSize);
  modelMap.addAttribute("pageInfo",pageInfo);

        return "system/dept/dept-list";
    }

    //添加-显示下拉框
@RequestMapping("/toAdd")
    public  String add(){

        String companyId=getLoginCompanyId();
        List<Dept> deptList=deptService.findDeptList(companyId);
        request.setAttribute("deptList",deptList);
        return "system/dept/dept-add(1)";
    }

    /**
     * 添加-保存数据
     * 因为添加和修改用的都是同一个对象，且返回的页面也相同，所以放一块操作，只需要判断即可
     * @param dept
     * @return
     */
   @RequestMapping("/edit")
    public String edit(Dept dept){

       dept.setCompanyId(getLoginCompanyId());
       dept.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(dept.getId())){
            //如果这个部门id为空，则为添加方法操作
            deptService.save(dept);
        }else {
            //否则就是修改操作
            deptService.update(dept);
        }

        return "redirect:/system/dept/list";
    }

    /**
     * 修改回显的数据
     * 功能入口：/system/dept/list
     * 请求地址：toUpdate
     * 请求参数：id
     * 响应数据：system/dept/dept-update
     * @return
     */
    @RequestMapping("/toUpdate")
    public String update(String id){
        //根据部门id,查询部门对象
      Dept dept=deptService.findById(id);
      //查询所有数据，作为下拉框数据
        List<Dept> deptList = deptService.findDeptList(getLoginCompanyId());
        request.setAttribute("dept",dept);
        request.setAttribute("deptList",deptList);

        return "system/dept/dept-update";
    }


    /**
     * 功能入口：/system/dept/list
     * 请求地址：delete
     * 请求参数：id
     * 返回值：{message : 1} 表示删除成功
     *
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> delete (String id){
        Map<String, Object> result = new HashMap<>();
        boolean flag=deptService.delete(id);
          if (flag){
              result.put("message","1");
          }else {
              result.put("message","0");
          }
        return result;
    }

}
